package com.wenbo.ocr.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Base64;

/**
 * @Author wenbo
 * @Date 2023/10/18 22:03
 **/
public class ImageUtils {

    public ImageUtils() {
    }

    public static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage newImage = new BufferedImage(width, height, 1);
        Image tmp = image.getScaledInstance(width, height, 4);
        Graphics2D g2d = newImage.createGraphics();
        g2d.drawImage(tmp, 0, 0, (ImageObserver)null);
        g2d.dispose();
        return newImage;
    }

    public static String toBase64(BufferedImage image, String formatName) {
        try {
            return Base64.getEncoder().encodeToString(toByteArray(image, formatName));
        } catch (Exception var3) {
            LogUtils.printMessage((String)null, var3, LogUtils.Level.ERROR);
            return null;
        }
    }

    public static byte[] toByteArray(BufferedImage image, String formatName) {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            byte[] var3;
            try {
                ImageIO.write(image, formatName, outputStream);
                var3 = outputStream.toByteArray();
            } catch (Throwable var6) {
                try {
                    outputStream.close();
                } catch (Throwable var5) {
                    var6.addSuppressed(var5);
                }

                throw var6;
            }

            outputStream.close();
            return var3;
        } catch (Exception var7) {
            LogUtils.printMessage((String)null, var7, LogUtils.Level.ERROR);
            return null;
        }
    }

    public static BufferedImage toImage(String base64) {
        try {
            return toImage(Base64.getDecoder().decode(base64));
        } catch (Exception var2) {
            LogUtils.printMessage((String)null, var2, LogUtils.Level.ERROR);
            return null;
        }
    }

    public static BufferedImage toImage(byte[] byteArray) {
        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray);

            BufferedImage var2;
            try {
                var2 = ImageIO.read(inputStream);
            } catch (Throwable var5) {
                try {
                    inputStream.close();
                } catch (Throwable var4) {
                    var5.addSuppressed(var4);
                }

                throw var5;
            }

            inputStream.close();
            return var2;
        } catch (Exception var6) {
            LogUtils.printMessage((String)null, var6, LogUtils.Level.ERROR);
            return null;
        }
    }

    public static BufferedImage toGray(BufferedImage image) {
        BufferedImage target = new BufferedImage(image.getWidth(), image.getHeight(), 10);
        Graphics2D g2d = target.createGraphics();
        g2d.drawImage(image, 0, 0, (ImageObserver)null);
        g2d.dispose();
        return target;
    }

    public static BufferedImage toBinary(BufferedImage image) {
        BufferedImage target = new BufferedImage(image.getWidth(), image.getHeight(), 12);
        Graphics2D g2d = target.createGraphics();
        g2d.drawImage(image, 0, 0, (ImageObserver)null);
        g2d.dispose();
        return target;
    }
}
